<template>
  <header class="header">
    <h1>todos</h1>
    <input
      id="toggle-all"
      class="toggle-all"
      type="checkbox"
      v-model="isAllDone"
    />
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      v-model.trim="content"
      @keyup.enter="handleSubmit"
    />
  </header>
</template>

<script setup lang="ts">
import { Todo } from "@/types";
import { ref, defineEmits, defineProps, computed } from "@vue/runtime-core";

const emit = defineEmits<{
  (event: "handleSubmit", value: Todo): void;
  (event: "isAllDone", value: boolean): void;
}>();

const props = defineProps<{ Todos: Todo[] }>();

const isAllDone = computed<boolean>({
  get: () => props.Todos.length !== 0 && props.Todos.every((v) => v.done),
  set: (value: boolean) => emit("isAllDone", value),
});

const content = ref("");

const handleSubmit = () => {
  content.value
    ? emit("handleSubmit", {
        id: Date.now(),
        content: content.value,
        done: false,
      })
    : alert("请输入");

  content.value = "";
};
</script>